package cc.zy.base.businesses.service.impl;

import cc.zy.base.businesses.entity.*;
import cc.zy.base.businesses.mapper.CollegeMapper;
import cc.zy.base.businesses.entity.Homework;
import cc.zy.base.businesses.entity.CollegeTerm;
import cc.zy.base.businesses.entity.Homework;
import cc.zy.base.businesses.mapper.HomeworkMapper;
import cc.zy.base.businesses.service.IHomeworkService;
import cc.zy.base.businesses.utils.ReadExcel;
import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.FebsResponse;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.SortUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 *  Service实现
 *
 * @author peizijian
 * @date 2021/01/29
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class HomeworkServiceImpl extends ServiceImpl<HomeworkMapper, Homework> implements IHomeworkService {
    @Resource
    private CollegeMapper collegeMapper;
    private final HomeworkMapper homeworkMapper;

    @Override
    public IPage<Homework> findHomeworks(QueryRequest request, Homework homework) {
        LambdaQueryWrapper<Homework> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        Page<Homework> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public List<Homework> findHomeworks(Homework homework) {

		return homeworkMapper.export(homework);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createHomework(Homework homework) {
        this.save(homework);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateHomework(Homework homework) {
        this.saveOrUpdate(homework);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHomework(Homework homework) {
        LambdaQueryWrapper<Homework> wrapper = new LambdaQueryWrapper<>();
	    // TODO 设置删除条件
	    this.remove(wrapper);
	}
    /**
     * 查询列表
     * @param homework
     * @author peizijian
     * @date 2021/01/29
     * @return
     */
    @Override
    public IPage<Homework> getHomeworkList(QueryRequest request, Homework homework) {
        Page<Homework> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countHomeworkDetail(homework));
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return this.baseMapper.getHomeworkList(page,homework);
    }

    /**
     * 学校下拉框
     * @return
     * @author peizijian
     * @date 2021/01/29
     */
    @Override
    public List<College> getCollegeList() {
        return collegeMapper.findCollegeDetail(new College());
    }

    /**
     * 通过学校id获取层次名称
     * @param collegeId
     * @author peizijian
     * @date 2021/01/29
     * @return
     */
    @Override
    public List<Level> getLevelListByCollegeId(Integer collegeId) {
        return homeworkMapper.getLevelListByCollegeId(collegeId);
    }
    /**
     * 通过学校和层次获取对应专业
     * @param collegeId
     * @param levelId
     * @author peizijian
     * @date 2021/01/29
     * @return
     */
    @Override
    public List<Major> getMajorListByIds(Integer collegeId, Integer levelId) {
        return homeworkMapper.getMajorListByIds(collegeId,levelId);
    }
    /**
     * 通过学校和层次及专业获取对应课程
     * @param collegeId
     * @param levelId
     * @param majorId
     * @author peizijian
     * @date 2021/01/29
     * @return
     */
    @Override
    public List<TeachProgram> getCourseNameByIds(Integer collegeId, Integer levelId, Integer majorId,Integer termId) {
        return homeworkMapper.getCourseNameByIds(collegeId, levelId, majorId,termId);
    }
    /**
     * 根据层次id获取对应学期
     * @param levelId
     *@author peizijian
     * @date 2021/01/29
     * @return
     */
    @Override
    public List<CollegeTerm> getTermsByLevelId(Integer levelId) {
        return homeworkMapper.getTermsByLevelId(levelId);
    }
    /**
     * 根据层次id获取对应学年
     * @param levelId
     * @author peizijian
     * @date 2021/01/29
     * @return
     */
    @Override
    public List<CollegeYear> getYearByLevelId(Integer levelId) {
        return homeworkMapper.getYearByLevelId(levelId);
    }
    /**
     * 根据id查找该学生已经上过的学期
     * @author hutengjiao
     * @date 2021/02/01
     */
    @Override
    public List<CollegeTerm> findTermByStuid(Integer id) {
        return this.homeworkMapper.selectTermByStuId(id);
    }
    /**
     * 根据id查找学习信息
     * @author hutengjiao
     * @date 2021/02/01
     */
    @Override
    public List<Homework> findHomeWorkBystuid(Integer id, Integer termId) {
        return this.homeworkMapper.selectHomeworkByStuId(id,termId);
    }
    /**
     * 根据ID查询学生作业
     * @param id
     * @author pzj
     * @date 2021/02/01
     * @return
     */
    @Override
    public Homework findHomeworkById(Integer id) {
        return this.homeworkMapper.findHomeworkById(id);
    }


}
